package com.itheima.crm.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;


import com.itheima.common.utils.Page;
import com.itheima.crm.pojo.BaseDict;

import com.itheima.crm.pojo.Organization;
import com.itheima.crm.pojo.RiZhi;
import com.itheima.crm.service.BaseDictService;

import com.itheima.crm.service.OrganizationService;
import com.itheima.crm.service.RiZhiService;
import com.itheima.crm.service.YuanGongService;

/**
 * 客户管理
 * 
 * @author lx
 *
 */
@Controller
public class OrganizationController {

	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private YuanGongService yuangongService;
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private RiZhiService rizhiService;
	
	// 注解在成员变量上

	public BaseDictService getBaseDictService() {
		return baseDictService;
	}

	public void setBaseDictService(BaseDictService baseDictService) {
		this.baseDictService = baseDictService;
	}

	public YuanGongService getYuangongService() {
		return yuangongService;
	}

	public void setYuangongService(YuanGongService yuangongService) {
		this.yuangongService = yuangongService;
	}

	public OrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public RiZhiService getRizhiService() {
		return rizhiService;
	}

	public void setRizhiService(RiZhiService rizhiService) {
		this.rizhiService = rizhiService;
	}

	@RequestMapping(value = "/organization")
	public String Organizationlist(Organization vo, Model model,HttpServletRequest request) {
		HttpSession session=request.getSession(false);
		String zhiwei = (String) session.getAttribute("zhiwei");
		
		if (!zhiwei.equals("1")) {
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");

		model.addAttribute("OrganizationType", OrganizationType);

		// 通过四个条件 查询分页对象
		Page<Organization> page = organizationService.selectOrganization(vo);
		model.addAttribute("page", page);
		yuangongService.updateManagerByOrganization();
		return "Organization";}
		
		else {
			
			return "error";
		}
	}

	@RequestMapping(value = "/organization/edit.action")
	public @ResponseBody Organization editOrganization(Integer id) {
		
		return organizationService.selectOrganizationById(id);
	}

	@RequestMapping(value = "/organization/insert.action")
	public @ResponseBody String insertOrganization(Organization organization,HttpServletRequest request) {
		organizationService.insertOrganization(organization);
		RiZhi a=new RiZhi();
		request.getSession().getAttribute("ygid");
		a.setYgid((Integer)request.getSession().getAttribute("ygid"));
		a.setYgname((String)request.getSession().getAttribute("ygname"));
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("添加部门信息");
		rizhiService.insertApplication(a);
		return "OK";
	}

	@RequestMapping(value = "/organization/delete.action")
	public @ResponseBody String deleteOrganization(Organization organization,HttpServletRequest request) {
		
	

		organizationService.deleteOrganization(organization);
		RiZhi a=new RiZhi();
		request.getSession().getAttribute("ygid");
		a.setYgid((Integer)request.getSession().getAttribute("ygid"));
		a.setYgname((String)request.getSession().getAttribute("ygname"));
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("删除部门信息");
		rizhiService.insertApplication(a);
		return "OK";
	}

	@RequestMapping(value = "/organization/update.action")
	public @ResponseBody String updateorganization(Organization organization,HttpServletRequest request) throws UnsupportedEncodingException {
		// 修改部门
		System.out.println(organization.getOname()+"---"+organization.getOnamenew());
		RiZhi a=new RiZhi();
		request.getSession().getAttribute("ygid");
		a.setYgid((Integer)request.getSession().getAttribute("ygid"));
		a.setYgname((String)request.getSession().getAttribute("ygname"));
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("更新部门信息");
		rizhiService.insertApplication(a);
		organizationService.updateOrganizationById(organization);
		
		return "OK";
	}

}
